package com.ebookstore.struts.action;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.Session;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ebookstore.dto.Cart;
import com.ebookstore.dto.CartItem;
import com.ebookstore.dto.Item;
import com.ebookstore.struts.form.CartForm;

public class CartAction extends BaseAction {
	/**
	 * 将图书添加到购物车
	 */
	public ActionForward addItem(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CartForm cartForm = (CartForm) form;
		//得到当前购物车对象
		Cart cart = cartForm.getCart();
		String itemId = cartForm.getItemId();
		System.out.println("CartAction.addItem():cart item id="+itemId);//id=5
		//更新购物车
		if (cart.containsItemId(itemId)) {//如果购物车中已存在该图书，则数量加1
			cart.incrementQuantityByItemId(itemId);
		} else {
			int id = -1;
			try {
				id = Integer.parseInt(itemId);//id=5
			} catch (Exception e) {
				e.printStackTrace();
			}
			//否则，将增加一个购物车明细
			Item item = this.getEBookStore().getCartService().findItemById(id);
			cart.addItem(item);//成功，item.itemID=5
//			System.out.println("第二次CartAction.addItem():cart item id="+item.getItemId());
		}
		return mapping.findForward("success");
	}
	
	/**
	 * 移除购物车明细
	 */
	public ActionForward removeItem(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CartForm cartForm = (CartForm) form;
//		System.out.println("CartAction.removeItem():itemnum="+cartForm.getCart().getNumberOfItems());
		Item ci=cartForm.getCart().removeItemById(cartForm.getItemId());
		if(ci!=null)
			System.out.println("CartAction.removeItem():item id= "+cartForm.getItemId());
		else {
			System.out.println("CartAction.removeItem():item = null");
		}
		return mapping.findForward("success");
	}
	/**
	 * 更新数量
	 */
	public ActionForward updateQuantity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CartForm cartForm = (CartForm) form;
		//获得购物车所有的明细
		Iterator cartItems = cartForm.getCart().getAllCartItems();
		while (cartItems.hasNext()) {
			CartItem cartItem = (CartItem) cartItems.next();
			String itemId = cartItem.getItem().getItemId() + "";
			try {
				int quantity = Integer.parseInt(request.getParameter("quantity_"+itemId));
				//重新设置明细数量
				cartForm.getCart().setQuantityByItemId(itemId, quantity);
				//如果是-1则移除
				if (quantity < 1) {
					cartItems.remove();
				}
			} catch (NumberFormatException e) {
			}
		}
		return mapping.findForward("success");
	}
	/**
	 * 结帐
	 */
	public ActionForward checkout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("checkout");
	}
	/**
	 * 查看购物车
	 */
	public ActionForward viewCart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			return mapping.findForward("confirm");
	}

}
